package org.techmore.biz;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.techmore.utils.BeeManager;
import org.techmore.utils.DownloadNoStoreHandler;
import org.techmore.utils.PropertiesUtil;
import org.techmore.vo.CouponCheckVO;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;

public class CaptureActivityDataHandler {
	
	private static CaptureActivityDataHandler captureActivityDataHandler = new CaptureActivityDataHandler();
	
	private static Activity activityInner;
	private static Context contextInner;
	public static CaptureActivityDataHandler getInstance(Activity activity, Context context){
		activityInner = activity;
		contextInner = context;
		beeManager = BeeManager.getInstance(activityInner, contextInner);
		return captureActivityDataHandler;
	}

	private CouponCheckVO couponCheckVO = null;
	private SAXParserFactory factory = null;
	private SAXParser parser = null;
	
	private Handler handler = null;
	
	private final String qr_coupon_check_url = PropertiesUtil.getInstance().getValue("qr_coupon_check_url");
	private String checkUrl = null;
	
	private static BeeManager beeManager = null;
	
	public void couponCheck(String couponNO, final TextView textView){
		
		couponCheckVO = new CouponCheckVO();
		
		checkUrl = qr_coupon_check_url+"&couponNo="+couponNO+"&account="+CouponUserInfo.userLocal.get();
		
		try {
			factory = SAXParserFactory.newInstance();
			parser = factory.newSAXParser();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			Log.e("ConsultCouponDetailsActivityDataHandler:", e.getMessage());
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			Log.e("ConsultCouponDetailsActivityDataHandler:", e.getMessage());
		}
		
		handler = new Handler(Looper.myLooper()){
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				int sign = msg.what;
    			if(sign == 1){
    				String uri = String.valueOf(msg.obj);
        			try {
						parser.parse(new ByteArrayInputStream(uri.getBytes()), new CouponCheckVO_handler());
					} catch (SAXException e) {
						Log.e("ConsultCouponDetailsActivityDataHandler:", e.getMessage());
					} catch (IOException e) {
						Log.e("ConsultCouponDetailsActivityDataHandler:", e.getMessage());
					}
					answerResponse(couponCheckVO.getCouponCheckoutString(),textView);
    			}else{
    				
    			}
			}
		};
		handler.post(new DownloadNoStoreHandler(checkUrl, handler, 1));
		
	}
	
	private void answerResponse(String couponCheckoutString, TextView textView){
		if("timeout".equals(couponCheckoutString)){
			textView.setText("已過期");
			beeManager.playStopBeepSoundAndVibrate(activityInner);
		}else if("valid".equals(couponCheckoutString)){
			textView.setText("有效");
			beeManager.playOKBeepSoundAndVibrate(activityInner);
		}else if("repeat".equals(couponCheckoutString)){
			textView.setText("已使用");
			beeManager.playStopBeepSoundAndVibrate(activityInner);
		}else if("null".equals(couponCheckoutString)){
			textView.setText("無效");
			beeManager.playStopBeepSoundAndVibrate(activityInner);
		}else{
			textView.setText("未識別");
			beeManager.playStopBeepSoundAndVibrate(activityInner);
		}
	}
	
	public class CouponCheckVO_handler extends DefaultHandler{
		public CouponCheckVO_handler(){}
		@Override
		public void startDocument() throws SAXException {
			super.startDocument();
		}
		@Override
		public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
			super.startElement(uri, localName, qName, attributes);
			
			if("coupon".equals(qName) || "coupon".equals(localName) ){
				couponCheckVO.setCouponCheckoutString(attributes.getValue("couponCheckout"));
			}
		}
		@Override
		public void endDocument() throws SAXException {
			super.endDocument();
		}
		@Override
		public void endElement(String uri, String localName, String qName) throws SAXException {
			super.endElement(uri, localName, qName);
			if("coupon".equals(qName) || "coupon".equals(localName)){
				
			}
		}
		@Override
		public void characters(char[] ch, int start, int length) throws SAXException {
		}
	}
	

}
